Okay, so welcome to the second project.
Um, in this section of the course today we are going to be creating some teleporter parts.
Now, um, just to clarify, before I continue on, there's kind of like two types of teleportation
in Roblox studio.
You have, um, teleportation between positions and in between places.
So um, position.
So we're making a teleporter based on position.
So it teleports you to a different position, whereas you can also get teleporters which teleport you
to different place.
Um, and if you're not sure about places and stuff yet and what that kind of means, we touched on that
in the advanced scripting section when we look at teleport service, um, which is how you teleport
between places.
But um, basically it kind of teleports you to another game within your game.
Basically.
Um, it's got a loading screen and all that kind of stuff.
Um, through that, we're not going to be doing that.
Um, we're looking at just using the position instead, but it's much simpler for now.
Okay, cool.
So like the other ones, um, I've got these teleporters from the toolbox.
Okay.
Doesn't really matter which ones you use as long as you've got two teleporters.
Um, it would probably help if you follow, um, the same naming as I've got in my explorer.
So I've made a folder with the two portals called these portal one and portal two.
Inside here there's some meshes and stuff, whatever.
Um, but the most important parts is the portal background and the teleport part here.
Okay.
Um, portal background is the, um, the background to the portal.
Um, whenever this is going to be touched, we're going to run our code, and the teleport part is where
the player will be, um, will be teleported to, um, so if they if they collide with this one, they've
teleported to the teleport part of, um, portal two.
The reason I did this is, um, through some testing, we'll explain a bit later on, but through some
testing, um, it didn't look as good as I wanted it to look.
So I added this feature in in in to make it look a little bit better.
Um, but yeah.
So that's what we're going to be taking a look at today.
Now, of course, um, you can either follow along, you can, um, give this a go yourself and refer
back to this lecture.
It's completely up to you.
Whichever way you find, um, is most is most beneficial for you.
Um, but I'm going to go ahead and get started in coding this.
This will be much shorter one with my traffic lights.
Um, a lot less code to write now.
Um, which I guess kind of also, um, highlights what I was explaining at the end of the previous project,
which is that, um, not writing tons of code isn't always the best.
Um, you see, um, scripts with like thousand plus lines of code.
They are not good scripts just because there's just lots of code.
There's not many good scripts.
The best scripts get the stuff done in the least amount of lines while it's still readable.
Of course.
Um, you know, if it's just if all of the code is on one line, um, that's not good code.
But if it's done in a, in a readable way, that's um, and little, little lines as possible.
And the lines aren't like, you know, really, really, really long.
That's what we really want to see.
Um, which is, um, what kind of what we're going to be doing today as well.
So, um, our code is much shorter.
Um, so it's a lot more efficient, a lot more readable.
Um, if you go back, it's much easier to see what's going on.
So overall, just much easier code to work with.
Okay.
All right.
Let's start off by making our variables.
So I'm going to make a variable for the portals folder which will just be the scripts parent.
I'm gonna make a variable for both of the portals.
So portal one is equal to portals dot portal one.
And then I can just copy that line down here.
Portal two.
Perfect.
I'm also going to add in a debounce.
And set this equal false.
To start off with.
Okay.
Um, we're going to be using the touch event.
So D bounces will come in handy a bit later on.
Okay.
So um this is going to be using predominantly events here.
Um we well we need let's think about how this is going to work.
When the player touches this portal, we want to move them over to this portal.
And when they touch this portal, we want them to move them over to this portal.
So take a minute to think about what we might be doing here.
Um, as there's one thing writing the code, but it's another thing actually figuring out, um, what
what you need to do in order to make this work.
Um, so have a think.
Maybe, um.
And, um, once you've got a bit of an idea, um, I'll let you know.
So we're going to be using two touch events.
One touch event will be for this board and the other one will be for that portal.
Of course going between them like that there.
Okay.
Um, we will also be using the um player added event as well.
Um, the reason being is because that gives us the name of the player, and because this has to be done
on the server, there's no other there's no other real efficient way to get the player player's name.
Um, rather rather than going through um, player added events, which is what we're going to be using.
Okay.
So start off with that.
Game dot players dot player added connect with a function and pass over the parameter player there.
Okay, um, so that gives us a player.
Um, we're going to want to make a variable up here for the character as, um, when the or.
Yeah.
When the.
Yeah.
So I guess I probably used I probably use the wrong terms earlier on with the player.
Really.
We want the character because the character is the physical object that's going to be colliding with
the portal.
The player isn't colliding with a portal.
The character is.
So we're wanting to move the character.
Player isn't a physical object.
Players exist in game players.
The character is what we want, what we're interested in here.
So we're assuming to reference the character now.
Um, now we know we looked on this earlier on, um, with local character.
Will be equal to player dot character.
However, it's not that simple as you remember.
Um, based on the fact that, um, if the character has not loaded yet, this will return nil, which
basically means nothing, so it won't work.
So we also had to have have to add a backup.
There's a couple different ways to do this.
Um, I showed you a couple of different ways.
The method I'm going to use for this is to use um or um player dot character added colon weight.
Now, um, I'm not sure if I showed you this, um, method of doing so.
Um, in the, in the lecture, I'm actually recording these, um, as newer lectures.
So it was a while ago now, a couple months ago that I recorded that one.
Um, so just in case I didn't explain it, what this basically does is, um, first off, we're using
an Or in a variable.
Looks weird.
Okay.
But basically, um, character will be equal to this, but yeah, it'll be equal to this by default.
But for some reason, if player character doesn't exist or there's any issues with it, it will instead
use the backup, which is this here.
So this is like a backup to this here basically.
Okay.
Um, player character added is an event, character added is event.
So when the character joins, when the character is fully loaded and everything's ready to go, that
event fires.
And we use colon weight.
Colon weight is kind of an alternative to colon connect.
Colon connect joins it to a function.
Colon weight will basically just have the script wait until that event fires.
So in other words waits until the character joins okay.
And then it will be equal to this here.
Okay.
So it's just, um, adding a backup.
And um, there are lots of different ways to do that.
Again, I think I showed you a couple of different ways, um, during the lecture, but, um, yeah,
it's something which is a bit of a pain to do sometimes.
Um, but yeah, once I've got the character, then, um, it should be nice and simple.
We want to just create two touched events, one for the first portal and one for the second portal.
So I'm going to do it for the first portal one.
Um, so portal one dot portal background as, um, it'll be this bit here.
Okay.
Um.
Dot touched.
Colon connect function.
We've hit as our other part.
Okay, hopefully these touch events are, um, really quick to, um, go over now as they are used a
lot.
They're probably the most common bit of code that I ever write.
In a touch event, they, um, very common.
So, um, you by the end of this course, you'll be able to write out extremely quickly and have it
know it off the top of your head.
Completely fine.
Um, but yeah.
Cool.
So, um, let's make sure that whatever collided with it has is a player.
Um, so it is a character and isn't like anything else.
Um, for example, um, this part here is colliding with this part here, but we don't want the code
to run because it's not a character, it's just a part.
So we're going to want to check if hit dot parent find first child.
With the name humanoid.
It has a humanoid inside of it.
It has to be a player.
It has to be a character.
So we can go ahead and run, run our code normally.
We also make sure that it's not debounce as well.
Um, you could say Debounce is equal to equal false, but it's much quicker to just write in short terms.
They're not debounce.
Um, this won't work if, um, the bounce is true, which we're going to set the amounts to be true
here, and the Debounce just prevents it from, um, occurring lots and lots of times in a row.
Um, with teleporters, you want to teleport the player once, and then, um, they might want to teleport
back.
Sure, but they wouldn't want to do that within 0.001 second of each other, like, um, so just make
sure that, um, because otherwise you might find that the player's being teleported back and forward,
back and forward and kind of like endless cycle.
Um, so adding a nice debounce in there just makes this a lot better.
Okay.
Um, this comes to the bit where I haven't explained yet, um, in any of the lectures, I don't believe,
which is how to actually move the character.
Okay, um, now, first of the character isn't something we've talked about too much.
It's a relatively new concept, but, um, let's just play here and might get an error or something.
But, um, if we do, just leave it.
Leave it, it's fine.
So my character here, um, we've got all the body parts and stuff here.
So the main question for this lecture is, well, how are we going to move the character?
Um, because we can move the character ourselves by using wasD, but we need to actually move them over
to that location.
Um, now have I think what how we might do this.
Um, now once one idea might be to move the humanoid root part.
This is like the main part, um, of the character.
We could try to move that.
Um, however, if we try to move it, set the position here to be like, um, 100, for example.
Um.
Oh, that does actually work.
I wasn't expecting that to work.
Um.
But apparently it does.
Okay.
Um.
That's annoying.
Um.
I didn't think that was going to work.
Um, right.
How?
Why is that work?
Yeah.
Right.
Okay.
Um, I basically I thought what was going to happen was I was going to move it, and a player was just
going to, um, going to die.
Because if you do that for any other, um, of the body parts.
So like here, for example, set the position to be 100.
Um.
It doesn't do anything, okay?
It doesn't work.
Um, but it did actually work when we went over.
We used humanoid root parts.
Now, the reason why we can't use that and just say humanoid root part dot position is equal to this
is based on the server and the client and the server.
And a client is something we explained right at the start of scripting.
Remember the server, um, controls what everybody sees.
My client controls what just one person sees.
Okay.
Now when you're in Roblox studio and you're playing the game, you're controlling the client.
Okay.
Um, anything you change in the explorer is controlling the client.
So if we go ahead and go over to the humanoid root part and, um, change it's change the humanoid parts
position to be a different position, it will have teleported you on your screen, but on everyone else's
screen, you will not have been teleported because it's made the change on your client, your your computer,
but hasn't made the change on anyone else's computer, hasn't changed your server.
So while you see that you've teleported, you're kind of just stuck there for everybody else.
Okay, um, which is a reason why we cannot use that.
Okay.
Didn't work the way I wanted it to work.
I wanted it to not work.
And then me to say, all you need to use this method, but, um, that's fine.
Um, so we still have the issue.
Then how are we going to make this work?
Well, we there is a, um, useful built in function that we're going to use called move to.
Okay.
And you can access this through the character so we can use character colon move two.
And this here moves the primary part to the given position in the primary part is not being specified
with root part of the model will be used.
Basically it will move the primary part of the model.
So the main the main part of the model.
And we can just put in here the vector free.
Sorry.
The position is the vector free.
Um we want to move it two.
So we want to move it to the second portal.
So portal two dot teleport dot position.
Okay.
Um, after that I want to put a weight on about 0.5 seconds and then set the bounce to be false again.
Again, just to make sure that it's only working once and it's not going to kind of try to teleport
them ten times at once.
Um, but yeah, now one thing you might be wondering as well, how how did you know that?
How did you know to use the move two?
Um, well, in truth, I've used it a lot of times before.
Um, but the first time I ever used it, I would have just searched it up on Google.
So a lot of these built in events and built in functions and stuff you will not know about.
Okay.
And I physically cannot teach you them.
I mean, um, this course is about 26, 27 hours at the moment.
Um, that I'm recording it, it would be 100 hours long if I was to explain every single thing in,
um, 100% detail and you'd be completely bored.
Okay.
Um, so, um, there's lots of these small kind of things, but you kind of have to figure out for yourself.
Now, you know what a built in function is?
You know what a function is.
So, um, it's as simple as just searching up, um, how how do you move the character in Roblox studio
via scripting?
Um, and it'll come up lots of search results, and you can have a look what kind of other people say,
um, but someone would have had your exact problem somewhere, and there'll be some kind of function
to fix it.
Okay, so whenever you're wanting to do something new that you're not sure, um, you know how to do
it, search it up and see if there is some kind of handy function or something else which, um, you
could use for this.
Okay.
Now, not everything you're going to be able to do, like, um, you might search up, um, something
and it comes up with some random stuff that you've never seen before.
Chances are Matt will probably will cover it later on in scripting, but if it is the case, it's just
a function, then you should be able to use that.
Fine.
But the point I'm basically saying is that, um, if you're not sure how to do something, just search
up, because often it's just an event, or it's just a function which you haven't seen before because
there are literally thousands of them.
Okay.
Um, cool.
So let's quickly go test this out.
Let's play the game.
And it should be able to teleport us from portal one to portal two.
So let's go through this portal.
And nice.
It's teleported us over to portal two.
That's perfect.
Obviously, we can't do it the same way.
I haven't coded that yet, but we do have that working.
Um, also, by the way, I have can collide off for all of these parts here just to make sure the player
can actually walk through the portal.
Um, you might get slightly different results if you have can collide on for those.
Okay.
Um, so from here then it's as simple as just copying this and changing around, um, portal one to
be portal two and portal two to be portal one.
Okay.
And that should work completely fine.
So if we go ahead and play this again, we can go from here to here completely fine.
And we can go back completely fine as well.
Okay.
Um, and we've got some nice simple teleporters and, um, the, the reason why this is, um, so yeah,
the reason why over here, we moved to the actual teleport part position instead of just saying, oh,
teleport to this vector three is that if you move the portal, the position of it's going to change.
So rather than hard coding the position value, you can instead just set it to be this position.
So no matter where this is, if you move it or um, whatever happens to it, um, it will move to that
position, not where it used to be, basically.
Okay.
Um, but yeah.
Cool.
Um, now one more thing I wanted to mention with this before we finish this project off is just the
result of it.
Um, so when I go through this portal, it kind of spawns me up into the air, and then I drop down.
Now, this is experimenting.
I couldn't actually find a way to get rid of this.
I mean, feel free if you want to take a look at, say like take a look at why as well.
Um, but I think this kind of always happens.
Um, the reason why I haven't encountered it as an issue before is that usually with teleporters and
stuff, um, you add like visual effects on the screen and other things like that, which kind of hide
what's going on.
Um, now visual effects and screen guys specifically, we haven't learned on how to code them just yet.
Um, so that won't be something we can add first time.
If you do get to the lecture and you want to decide to go back and add the GUI stuff onto this, which
would be very cool, add a nice little GUI which popped up on the screen, which kind of fades in and
out so it looks like you're teleporting, or to add some kind of other visual effect as well.
Feel free to experiment with this.
Um, but we have the base kind of teleporter working here.
Okay.
Um, cool.
So we'll leave it there then.
So I hope you enjoyed, uh, making these cool teleporters.
Teleporters are quite a big thing for a lot of games.
Um, so hopefully this will be useful.
And, um, you've learned a bit about how to, um, combine these and stuff you play.
Add is a very useful as you've seen here.
Um, by just using it simply just to get the player.
Um, is, is a very good use for it.
Um, but yeah, the debounce I just use one debounce instead of using two d bounces.
Um, the reason is because.
If, um.
I don't want them to.
I don't want them to use the portal again, if that makes sense.
If I have two separate de balances.
Um, if it's weighted, if you weighted 0.5 seconds, you could technically go this portal to this portal
to back, back, back, back, back, and so on.
Um, because it's, they're kind of you only have to wait 0.5 seconds to use this one again, but you
can use this one straight after, if that makes sense.
So by having just one, it means that no matter which one you're on, you have to wait 0.5 seconds.
Okay.
Um, in case you're wondering why I just use 1D bounce for two events here.
Okay.
Um, but yeah.
Cool.
So again, hope you enjoyed this one.
And, um, keep going through the rest of the course.
There'll be one more project coming up, um, for scripting.
Um, but that'll be it.
So we're kind of getting towards the end of the, um, basic side of scripting, I would say, um,
but yeah, keep enjoying, keep going through, um, the rest of the course.